/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.freemarker.directive;

import vip.dexian.common.utils.FreeMarkerUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页指令
 *
 * @author 挺好的 2023年05月30日 15:51
 */
@Component ("paginationDirective")
public class PaginationDirective extends BaseDirective {

    /**
     * 规则
     */
    private static final String PATTERN_PARAMETER_NAME = "pattern";

    /**
     * 页码
     */
    private static final String PAGE_NUMBER_PARAMETER_NAME = "pageNumber";

    /**
     * 总页数
     */
    private static final String TOTAL_PAGES_PARAMETER_NAME = "totalPages";

    /**
     * 分片数量
     */
    private static final String SEGMENT_COUNT_PARAMETER_NAME = "segmentCount";

    /**
     * 规则变量名称
     */
    private static final String PATTERN_VARIABLE_NAME = "pattern";

    /**
     * 页码变量名称
     */
    private static final String PAGE_NUMBER_VARIABLE_NAME = "pageNumber";

    /**
     * 总页码
     */
    private static final String PAGE_COUNT_VARIABLE_NAME = "totalPages";

    /**
     * 分片
     */
    private static final String SEGMENT_COUNT_VARIABLE_NAME = "segmentCount";

    /**
     * 是否有上一页
     */
    private static final String HAS_PREVIOUS_VARIABLE_NAME = "hasPrevious";

    /**
     * 是否有下一页
     */
    private static final String HAS_NEXT_VARIABLE_NAME = "hasNext";

    /**
     * 是否首页
     */
    private static final String IS_FIRST_VARIABLE_NAME = "isFirst";

    /**
     * 是否最后一页
     */
    private static final String IS_LAST_VARIABLE_NAME = "isLast";

    /**
     * 上一页页码
     */
    private static final String PREVIOUS_PAGE_NUMBER_VARIABLE_NAME = "previousPageNumber";

    /**
     * 下一页页码
     */
    private static final String NEXT_PAGE_NUMBER_VARIABLE_NAME = "nextPageNumber";

    /**
     * 首页页码
     */
    private static final String FIRST_PAGE_NUMBER_VARIABLE_NAME = "firstPageNumber";

    /**
     * 最后一页页码
     */
    private static final String LAST_PAGE_NUMBER_VARIABLE_NAME = "lastPageNumber";

    /**
     * 分片
     */
    private static final String SEGMENT_VARIABLE_NAME = "segment";

    /**
     * 执行标签内容
     *
     * @param env
     * @param params
     * @param loopVars
     * @param body
     *
     * @throws freemarker.template.TemplateException
     * @throws java.io.IOException
     */
    @Override
    @SuppressWarnings ({"unchecked", "rawtypes"})
    public void execute (Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {

        String pattern = FreeMarkerUtils.getParameter(PaginationDirective.PATTERN_PARAMETER_NAME, String.class, params);
        Integer pageNumber = FreeMarkerUtils.getParameter(PaginationDirective.PAGE_NUMBER_PARAMETER_NAME, Integer.class,
                params
        );
        Integer totalPages = FreeMarkerUtils.getParameter(PaginationDirective.TOTAL_PAGES_PARAMETER_NAME, Integer.class,
                params
        );
        Integer segmentCount = FreeMarkerUtils.getParameter(PaginationDirective.SEGMENT_COUNT_PARAMETER_NAME,
                Integer.class, params
        );

        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
        }
        if (totalPages == null || totalPages < 1) {
            totalPages = 1;
        }
        if (segmentCount == null || segmentCount < 1) {
            segmentCount = 5;
        }
        boolean hasPrevious = pageNumber > 1;
        boolean hasNext = pageNumber < totalPages;
        boolean isFirst = pageNumber == 1;
        boolean isLast = pageNumber.equals(totalPages);
        int previousPageNumber = pageNumber - 1;
        int nextPageNumber = pageNumber + 1;
        int firstPageNumber = 1;
        int lastPageNumber = totalPages;
        int startSegmentPageNumber = pageNumber - (int) Math.floor((segmentCount - 1) / 2D);
        int endSegmentPageNumber = pageNumber + (int) Math.ceil((segmentCount - 1) / 2D);
        if (startSegmentPageNumber < 1) {
            startSegmentPageNumber = 1;
        }
        if (endSegmentPageNumber > totalPages) {
            endSegmentPageNumber = totalPages;
        }
        List <Integer> segment = new ArrayList <>();
        for (int i = startSegmentPageNumber; i <= endSegmentPageNumber; i++) {
            segment.add(i);
        }

        Map <String, Object> variables = new HashMap <>();
        variables.put(PaginationDirective.PATTERN_VARIABLE_NAME, pattern);
        variables.put(PaginationDirective.PAGE_NUMBER_VARIABLE_NAME, pageNumber);
        variables.put(PaginationDirective.PAGE_COUNT_VARIABLE_NAME, totalPages);
        variables.put(PaginationDirective.SEGMENT_COUNT_VARIABLE_NAME, segmentCount);
        variables.put(PaginationDirective.HAS_PREVIOUS_VARIABLE_NAME, hasPrevious);
        variables.put(PaginationDirective.HAS_NEXT_VARIABLE_NAME, hasNext);
        variables.put(PaginationDirective.IS_FIRST_VARIABLE_NAME, isFirst);
        variables.put(PaginationDirective.IS_LAST_VARIABLE_NAME, isLast);
        variables.put(PaginationDirective.PREVIOUS_PAGE_NUMBER_VARIABLE_NAME, previousPageNumber);
        variables.put(PaginationDirective.NEXT_PAGE_NUMBER_VARIABLE_NAME, nextPageNumber);
        variables.put(PaginationDirective.FIRST_PAGE_NUMBER_VARIABLE_NAME, firstPageNumber);
        variables.put(PaginationDirective.LAST_PAGE_NUMBER_VARIABLE_NAME, lastPageNumber);
        variables.put(PaginationDirective.SEGMENT_VARIABLE_NAME, segment);
        this.setLocalVariables(variables, env, body);
    }
}
